<?php
/**
 * 分组权限相关业务
 */

namespace app\api\business;
use app\common\model\mysql\AdminRule as AdminRuleModel;
use think\Exception;
class AdminRoleBus extends BaseBus
{
    /**
     * 用户列表
     * @param array $data
     * @return array
     * @throws Exception
     */
    public static function getIdList()
    {
        try {
            // 限定条件
            $where = array();
            // 需要显示的字段，默认空全部显示
            $displayField=['id'];
            // 需要排除显示的字段，默认空全部显示,优先级大
            $hideField = [];
            // 排序
            $order = [ "id"=>"desc"];
            $role_menu = new AdminRuleModel();
            // 查询数据
            $res = $role_menu->listDataSql($where,$displayField,$hideField,$order);
            if(empty($res)){
                return array();
            }
            $ids = [];
            foreach ($res->toArray() as $value){
                array_push($ids,$value['id']);
            }
            return $ids;
        }catch (\Exception $e){
            exception($e,'AdminRoleBus.listData');
        }
    }

    /**
     * 获取多个数据
     * @return void
     */
    public static function getMultipleData($where)
    {
        try{
            $role_menu = new AdminRuleModel();
            $res = $role_menu->getMultipleData($where);
            if(empty($res)){
                return false;
            }
            return $res->toArray();
        } catch(\Exception $e) {
            exception($e,'MenuBus.getMultipleData');
        }
    }

    /**
     * 无限极分类
     * @param $menuList
     * @param int $parentId
     * @return array
     */
    public static function getTreeData($menuList, int $parentId = 0)
    {
        try {
            $data = [];
            foreach ($menuList as $key => $item) {
                if ($item['pid'] == $parentId) {
                    $children = self::getTreeData($menuList, (int)$item['id']);
                    if($children){
                        $item['children'] = $children;
                    }
                    $data[] = $item;
                    unset($menuList[$key]);
                }
            }
            return $data;
        }catch (\Exception $e){
            exception('MenuBus.getTreeData',$e);
        }
    }
}
